package com.frank.libandroid.butterknife;

import android.os.Build;
import android.view.View;

import androidx.annotation.RequiresApi;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;

import com.frank.libandroid.R;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

public class ButterKnife {
    public static void bind(Object object, View view) {
        Class<?> aClass = object.getClass();
        for (Field field : aClass.getDeclaredFields()) {
            BindAdapter annotation = field.getAnnotation(BindAdapter.class);
            if (annotation != null) {
                int value = annotation.value();
                RecyclerView recyclerView = view.findViewById(value);
                try {
                    field.setAccessible(true);
                    recyclerView.setAdapter((RecyclerView.Adapter) field.get(object));
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        Method[] methods = aClass.getDeclaredMethods();
        for (Method method : methods) {
            OnClick annotation = method.getAnnotation(OnClick.class);
            if (annotation != null) {
                int value = annotation.value();
                view.findViewById(value).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try {
                            method.setAccessible(true);
                            method.invoke(object);
                        } catch (IllegalAccessException e) {
                            throw new RuntimeException(e);
                        } catch (InvocationTargetException e) {
                            throw new RuntimeException(e);
                        }
                    }
                });
            }
        }
    }

    public static <V extends ViewBinding> void bindMultiAdapter(Object object, V binding) {
        Class<?> aClass = object.getClass();
        for (Method method : aClass.getDeclaredMethods()) {
            OnClick annotation = method.getAnnotation(OnClick.class);
            if (annotation != null) {
                int value = annotation.value();
//                Class type = annotation.type();
//                if (binding.getClass() == type) {
//                    binding.getRoot().findViewById(value).setOnClickListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(View v) {
//                            try {
//                                method.setAccessible(true);
//                                method.invoke(object, v, binding.getRoot().getTag(R.id.tag_bean));
//                            } catch (IllegalAccessException e) {
//                                throw new RuntimeException(e);
//                            } catch (InvocationTargetException e) {
//                                throw new RuntimeException(e);
//                            }
//                        }
//                    });
//                }
            }
            BindData bindData = method.getAnnotation(BindData.class);
            if (bindData != null) {
//                Class value = bindData.value();
//                if (value == binding.getClass()) {
//                    binding.getRoot().setTag(R.id.tag_bindMethod, method);
//                }
            }
        }
    }

    public static <V extends ViewBinding> void bindAdapter(Object object, V binding) {
        Class<?> aClass = object.getClass();
        for (Method method : aClass.getDeclaredMethods()) {
            OnClick annotation = method.getAnnotation(OnClick.class);
            if (annotation != null) {
                int value = annotation.value();
                binding.getRoot().findViewById(value).setOnClickListener(new View.OnClickListener() {
                    @RequiresApi(api = Build.VERSION_CODES.O)
                    @Override
                    public void onClick(View v) {
                        try {
                            method.setAccessible(true);
                            Parameter[] parameters = method.getParameters();
                            Object[] args = new Object[parameters.length];
                            for (int i = 0; i < parameters.length; i++) {
                                Class<?> type = parameters[i].getType();
                                if (ViewDataBinding.class.isAssignableFrom(type)) {
                                    args[i] = binding;
                                } else if (type == int.class) {
                                    args[i] = binding.getRoot().getTag(R.id.tag_position);
                                } else {
                                    args[i] = binding.getRoot().getTag(R.id.tag_bean);
                                }
                            }
                            method.invoke(object, args);
                        } catch (IllegalAccessException e) {
                            throw new RuntimeException(e);
                        } catch (InvocationTargetException e) {
                            throw new RuntimeException(e);
                        }
                    }
                });
            }
        }
    }
}
